package com.dsideal.cloud.cas.client.controller;

import com.dsideal.cloud.auth.core.annotation.IgnoreUserToken;
import com.dsideal.cloud.cache.service.RedisService;
import com.dsideal.cloud.cas.client.cas.properties.CasConfiguration;
import com.dsideal.cloud.common.response.BaseResponse;
import com.dsideal.cloud.common.response.ObjectRestResponse;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.security.Principal;

/**
 * 登出控制器
 *
 * @author suny
 */
@Controller
@Slf4j
public class LogoutController {

    @Autowired
    private CasConfiguration casConfiguration;

    @Autowired
    private RedisService redisService;

    /**
     * CAS登出，注销Spring Security，移除REDIS中相关用户
     */
    @RequestMapping("/logout")
    public String logout(Principal principal, String username, Model model) {
        log.info(" cas logout...");
//         cas logout
        SecurityContextHolder.getContext().setAuthentication(null);
        SecurityContextHolder.clearContext();
//        model.addAttribute("url",casConfiguration.getCasServerLoginUrl());
        return "redirect:"+casConfiguration.getCasServerLogoutUrl();
    }

    @GetMapping("/cluster")
    @IgnoreUserToken
    @ResponseBody
    public ObjectRestResponse cluster() {
        return ObjectRestResponse.ok(redisService.get("pri"));
    }


}
